<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :onRowClick="handleClick" :onRowDbClick="handleDblclick" :rowClassName="rowClassName">
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" style="margin-top: 8px; float: right;"> 新增</a-button>
      </template>
      <!--产品-->
      <template #description="{ text, record }">
        <a-tag color="red" v-if="text" v-for="(item, index) in text.split(',')" style="margin-right: 5px;">{{item}}</a-tag>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>

    <!--表单区域 -->
    <ProductModal @register="registerModal" @success="handleSuccess"></ProductModal>

    <!--产品抽屉-->
    <ProductItemList @register="registerDrawer" @success="handleSuccess"></ProductItemList>
  </div>
</template>

<script lang="ts" name="product-list" setup>
  import { ref, computed, unref, reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './Product.data';
  import { list, deleteOne } from './Product.api';
  import ProductModal from './modules/ProductModal.vue';
  import ProductItemList from './modules/ProductItemList.vue';

  //初始化 -------------------------------------------------------------
  //定义查询表单，查询条件
  const queryParam = reactive<any>({
    capitalName: '',
    productName: ''
  });

  //注册model
  const [registerModal, { openModal }] = useModal();

  const [registerDrawer, { openDrawer }] = useDrawer();

  //注册table数据
  const { prefixCls, tableContext} = useListPage({
    tableProps: {
      api: list,
      columns: columns,
      canResize: true,
      showIndexColumn: true,
      showTableSetting: false,
      useSearchForm: false,
      actionColumn: {
        width: 220,
      },
    },
  });

  const [registerTable, { reload, setProps }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 详情
   */
  function handleItem(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        color: "success",
        label: "产品配置",
        onClick: handleItem.bind(null, record)
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        color: "error",
        label: "删除",
        popConfirm: {
          title: "确定删除吗?",
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }

  //单击行
  let selectRows = reactive<any>({id: ''});
  function handleClick(record) {
    selectRows.id = record.id;
  }

  //双击行
  function handleDblclick(record) {
    handleEdit(record);
  }

  //选择行
  function rowClassName(record, index) {
    return record.id === selectRows.id ? 'row-selected' : '';
  }
</script>
<style scoped></style>
